package trading.algo.BullToBearTask;

import java.util.Timer;
import java.util.TimerTask;

import trading.algo.TimerKillerTask;
import trading.bo.IBAccessorBO;
import trading.util.MarketTimeHelper;
import trading.util.VIXFrontMonthCheckUtil;

public class VIXFutureGetDataTask extends TimerTask {

	private MarketTimeHelper marketTimeHelper;
	private IBAccessorBO ibAccessorBO;
	
	public VIXFutureGetDataTask(IBAccessorBO ibAccessorBO, MarketTimeHelper marketTimeHelper) {
		this.marketTimeHelper = marketTimeHelper;
	    this.ibAccessorBO = ibAccessorBO;
	}
	
	@Override
	public void run() {
		String frontMonthPrice = VIXFrontMonthCheckUtil.getVIXFrontMonthPrice();
		VIXFrontMonthCheckUtil.finishTestingFrontMonthDate();
		System.out.println("Finish testing VIX future front month date");
		
		Timer timer = new Timer();
		int taskID = 0;
		// Current month is not front month
		if (frontMonthPrice == null) {
			System.out.println("Current month is not VIX future front month.");
			VIXFuturePriceTask vixFuturePriceTask = new VIXFuturePriceTask(this.ibAccessorBO, this.marketTimeHelper.getNextNextMonthExpiration());
			timer.schedule(vixFuturePriceTask, taskID * 1000);
			taskID += 3;
		} else {
			// Current month is front month
			System.out.println("Current month is VIX future front month.");
			VIXFuturePriceTask vixFuturePriceTask = new VIXFuturePriceTask(this.ibAccessorBO, this.marketTimeHelper.getNextMonthExpiration());
			timer.schedule(vixFuturePriceTask, taskID * 1000);
			taskID += 3;
		}
		TimerKillerTask timerKillerTask = new TimerKillerTask(timer);
		timer.schedule(timerKillerTask, taskID * 1000);
	}

}
